package org.takeback.chat.entity;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.takeback.chat.lottery.listeners.GameException;
import org.takeback.chat.store.room.Room;
import org.takeback.chat.utils.CheckIsInUtils;

import com.alibaba.fastjson.annotation.JSONField;

public class LHHModel {

	private BigDecimal ziro;
	private BigDecimal one;
	private BigDecimal two;
	private BigDecimal three;
	private BigDecimal four;
	private BigDecimal five;
	private BigDecimal six;
	private BigDecimal seven;
	private BigDecimal eight;
	private BigDecimal nine;
	private BigDecimal longMoney;
	private BigDecimal huMoney;
	private BigDecimal heMoney;
	private List<LHHModelzh> zh;
	
	@JSONField(serialize=false)
	private Room room;
	@JSONField(serialize=false)
	private String errMsg;
	
	public String getErrMsg() {
		return errMsg;
	}
	public void setErrMsg(String errMsg) {
		this.errMsg = errMsg;
	}

	private Map<String, Object> p;
	
	public Map<String, Object> getP() {
		return p;
	}
	public void setP(Map<String, Object> p) {
		this.p = p;
	}
	public Room getRoom() {
		return room;
	}
	public void setRoom(Room room) {
		this.room = room;
	}
	public BigDecimal getZiro() {
		return ziro;
	}
	public void setZiro(BigDecimal ziro) {
		this.ziro = ziro;
	}
	public BigDecimal getOne() {
		return one;
	}
	public void setOne(BigDecimal one) {
		this.one = one;
	}
	public BigDecimal getTwo() {
		return two;
	}
	public void setTwo(BigDecimal two) {
		this.two = two;
	}
	public BigDecimal getThree() {
		return three;
	}
	public void setThree(BigDecimal three) {
		this.three = three;
	}
	public BigDecimal getFour() {
		return four;
	}
	public void setFour(BigDecimal four) {
		this.four = four;
	}
	public BigDecimal getFive() {
		return five;
	}
	public void setFive(BigDecimal five) {
		this.five = five;
	}
	public BigDecimal getSix() {
		return six;
	}
	public void setSix(BigDecimal six) {
		this.six = six;
	}
	public BigDecimal getSeven() {
		return seven;
	}
	public void setSeven(BigDecimal seven) {
		this.seven = seven;
	}
	public BigDecimal getEight() {
		return eight;
	}
	public void setEight(BigDecimal eight) {
		this.eight = eight;
	}
	public BigDecimal getNine() {
		return nine;
	}
	public void setNine(BigDecimal nine) {
		this.nine = nine;
	}

	public BigDecimal getLongMoney() {
		return longMoney;
	}
	public void setLongMoney(BigDecimal longMoney) {
		this.longMoney = longMoney;
	}
	
	public BigDecimal getHuMoney() {
		return huMoney;
	}
	public void setHuMoney(BigDecimal huMoney) {
		this.huMoney = huMoney;
	}
	
	public BigDecimal getHeMoney() {
		return heMoney;
	}
	public void setHeMoney(BigDecimal heMoney) {
		this.heMoney = heMoney;
	}
	
	/*
	 * 获取押注的所有金额
	 * @return
	 */
	@JSONField(serialize=false)
	public BigDecimal getTotleMoney() {
		BigDecimal b = new BigDecimal(0);
		if(this.ziro != null)  b = b.add(this.ziro);
		if(this.one != null)  b = b.add(this.one);
		if(this.two != null)  b = b.add(this.two);
		if(this.three != null)  b = b.add(this.three);
		if(this.four != null)  b = b.add(this.four);
		if(this.five != null)  b = b.add(this.five);
		if(this.six != null)  b = b.add(this.six);
		if(this.seven != null)  b = b.add(this.seven);
		if(this.eight != null)  b = b.add(this.eight);
		if(this.nine != null)  b = b.add(this.nine);
		
		if(this.longMoney != null)  b = b.add(this.longMoney);
		if(this.huMoney != null)  b = b.add(this.huMoney);
		if(this.heMoney != null)  b = b.add(this.heMoney);
		//添加组合的钱
		BigDecimal zhMoney = getCheckZhCountMoney();
		if(zhMoney != null) {
			b = b.add(zhMoney);
		}
		System.out.println(b.doubleValue());
		if(b.compareTo(BigDecimal.ZERO) == 0) throw new RuntimeException("金额错误");
		return b;
	}
	
	//校验单注金额是否正确
	@JSONField(serialize=false)
	public String checkPlanOne() throws GameException {
		if(this.p == null) throw new GameException(500, "系统异常"); 
		BigDecimal min = new BigDecimal(this.p.get("config_D_min").toString());
		BigDecimal max = new BigDecimal(this.p.get("config_D_max").toString());
		errMsg = null;
		if(this.ziro != null) {
			if(this.ziro.compareTo(max) == 1 || this.ziro.compareTo(min)  == -1) {
				return errMsg = "单注0的金额错误， 单注最少"+min.doubleValue()+"微币，最大"+max.doubleValue()+"微币！";
			} 
		}
		if(this.one != null) {
			if(this.one.compareTo(max) == 1 || this.one.compareTo(min)  == -1) {
				return errMsg = "单注1的金额错误，单注最少"+min.doubleValue()+"微币，最大"+max.doubleValue()+"微币！";
			} 
		}
		if(this.two != null) {
			if(this.two.compareTo(max) == 1 || this.two.compareTo(min)  == -1) {
				return errMsg = "单注2的金额错误，单注最少"+min.doubleValue()+"微币，最大"+max.doubleValue()+"微币！";
			} 
		}
		if(this.three != null) {
			if(this.three.compareTo(max) == 1 || this.three.compareTo(min)  == -1) {
				return errMsg = "单注3的金额错误，单注最少"+min.doubleValue()+"微币，最大"+max.doubleValue()+"微币！";
			} 
		}
		if(this.four != null) {
			if(this.four.compareTo(max) == 1 || this.four.compareTo(min)  == -1) {
				return errMsg = "单注4的金额错误， 单注最少"+min.doubleValue()+"微币，最大"+max.doubleValue()+"微币！";
			} 
		}
		if(this.five != null) {
			if(this.five.compareTo(max) == 1 || this.five.compareTo(min)  == -1) {
				return errMsg = "单注5的金额错误，单注最少"+min.doubleValue()+"微币，最大"+max.doubleValue()+"微币！";
			} 
		}
		if(this.six != null) {
			if(this.six.compareTo(max) == 1 || this.six.compareTo(min)  == -1) {
				return errMsg = "单注6的金额错误， 单注最少"+min.doubleValue()+"微币，最大"+max.doubleValue()+"微币！";
			} 
		}
		if(this.seven != null) {
			if(this.seven.compareTo(max) == 1 || this.seven.compareTo(min)  == -1) {
				return errMsg = "单注7的金额错误，单注最少"+min.doubleValue()+"微币，最大"+max.doubleValue()+"微币！";
			} 
		}
		if(this.eight != null) {
			if(this.eight.compareTo(max) == 1 || this.eight.compareTo(min)  == -1) {
				return errMsg = "单注8的金额错误， 单注最少"+min.doubleValue()+"微币，最大"+max.doubleValue()+"微币！";
			} 
		}
		if(this.nine != null) {
			if(this.nine.compareTo(max) == 1 || this.nine.compareTo(min)  == -1) {
				return errMsg = "单注9的金额错误， 单注最少"+min.doubleValue()+"微币，最大"+max.doubleValue()+"微币！";
			} 
		}

		return errMsg;
	}
	
	//校验组合二
	@JSONField(serialize=false)
	public BigDecimal getCheckZhCountMoney() {
		BigDecimal b = new BigDecimal(0);
		if(this.zh == null) return null;
		this.errMsg = null;
		for(LHHModelzh l : this.zh){
			String key =  l.getType() == 4 || l.getType() == 5 ? "45" : l.getType().toString();
			BigDecimal planmin = new BigDecimal(p.get("config_Z" + key + "_min").toString());
			BigDecimal planmax = new BigDecimal(p.get("config_Z" + key + "_max").toString());
			
			if(l.getType() == 2) {//组合2
				if(l.getNum1() == null && l.getNum2() == null && l.getMoney() == null) continue;  
				if(!CheckIsInUtils.checkNum(l.getNum1(), l.getNum2())) {
					errMsg = "请检查连码2的数字，只能是0到9";
				}
			} else if(l.getType() == 3) {
				if(l.getNum1() == null && l.getNum2() == null && l.getNum3() == null && l.getMoney() == null) continue;  
				if(!CheckIsInUtils.checkNum(l.getNum1(), l.getNum2(), l.getNum3())) {
					errMsg = "请检查连码3的数字，只能是0到9";
				}
			} else if(l.getType() == 4) {
				if(l.getNum1() == null && l.getNum2() == null && l.getNum3() == null && l.getNum4() == null && l.getMoney() == null) continue;
				if(!CheckIsInUtils.checkNum(l.getNum1(), l.getNum2(),l.getNum3(), l.getNum4())) {
					errMsg = "请检查连码4的数字，只能是0到9";
				}
			} else if(l.getType() == 5) {
				if(l.getNum1() == null && l.getNum2() == null && l.getNum3() == null && l.getNum4() == null && l.getNum5() == null && l.getMoney() == null) continue;
				if(!CheckIsInUtils.checkNum(l.getNum1(), l.getNum2(), l.getNum3(),l.getNum4(), l.getNum5())) {
					errMsg = "请检查连码5的数字，只能是0到9";
				}
			}
			if(planmin.compareTo(l.getMoney()) == 1 || planmax.compareTo(l.getMoney())  == -1) {
				this.errMsg = "组合"+l.getType()+"的金额错误， 单注最少"+planmin.doubleValue()+"微币，最大"+planmax.doubleValue()+"微币！";
			} 
			if(errMsg != null) {
				return b = null;
			}
			b = b.add(l.getMoney());
		}
		return b;
	}
	
	public static void main(String[] args) {
		System.out.println(CheckIsInUtils.checkNum(1,10));
		BigDecimal a = new BigDecimal("50");
		BigDecimal b = new BigDecimal("5000");
		BigDecimal d = new BigDecimal("20");
		System.out.println(a.compareTo(b));//-1   20   5000
		System.out.println(a.compareTo(d));//0
		if(a.compareTo(b) == 1  || a.compareTo(d) == -1) {
			System.out.println(">>>>>>");
		} 
	}
	
	/**
	 * 龙虎合的金额校验
	 * @return
	 */
	@JSONField(serialize=false)
	public Boolean checkLongHuHe() {
		BigDecimal planmin = new BigDecimal(p.get("config_LHH_min").toString());
		BigDecimal planmax = new BigDecimal(p.get("config_LHH_max").toString());
		if(this.getLongMoney() != null && (planmin.compareTo(this.getLongMoney())  == 1|| planmax.compareTo(this.getLongMoney())  == -1)) {
			this.setErrMsg("龙的金额错误，龙虎合最少" + planmin.doubleValue()+",最大" + planmax.doubleValue()+"微币");
			return false;
		} 
		if(this.getHuMoney() != null && (planmin.compareTo(this.getHuMoney()) == 1 || planmax.compareTo(this.getHuMoney())  == -1)) {
			this.setErrMsg("虎的金额错误，龙虎合最少" + planmin.doubleValue()+",最大" + planmax.doubleValue()+"微币");
			return false;
		} 
		if(this.getHeMoney() != null && (planmin.compareTo(this.getHeMoney()) == 1 || planmax.compareTo(this.getHeMoney())  == -1)) {
			this.setErrMsg("合的金额错误，龙虎合最少" + planmin.doubleValue()+",最大" + planmax.doubleValue()+"微币");
			return false;
		} 
		return true;
	}
	
	@JSONField(serialize=false)
	public boolean checkZHPass() {
		return this.getCheckZhCountMoney() != null;
	}
	
	public List<LHHModelzh> getZh() {
		return zh;
	}
	public void setZh(List<LHHModelzh> zh) {
		this.zh = zh;
	}
}
